<template>
  <div class="app-main" v-loading="loading">
    <div class="divTmp" v-show="loading"></div>
    <div class="index-body" v-show="!loading">
      <div class="ko-view">
        <div class="form-wrapper" id="form-stu">
          <div class="clearfix">
            <div class="user-info-title mt40">学校信息</div>
            <div class="form-group clearfix">
              <div class="user-info-label">
                <i style="margin-right: 5px;color: red">*</i>
                <span>所在院校</span>
              </div>
              <div class="pull-left gray school">
                {{ user.college }}
              </div>
            </div>
            <div class="form-group clearfix">
              <div class="user-info-label">
                <span>年级</span>
              </div>
              <div class="pull-left gray school">
                {{ user.grade }}
              </div>
            </div>
            <div class="form-group clearfix">
              <div class="user-info-label">

                <span>学院</span>
              </div>
              <div class="pull-left gray school">
                {{ user.academy }}
              </div>
            </div>
            <div class="form-group clearfix">
              <div class="user-info-label">
                <span>自然班级</span>
              </div>
              <div class="pull-left gray school">
                {{ user.classes }}
              </div>
            </div>
            <div class="form-group clearfix">
              <div class="user-info-label" style="margin-top: 5px">
                <i style="margin-right: 5px;color: red">*</i>
                <span>学号</span>
              </div>
              <el-input name="number" class="text-input" type="text"
                        maxlength="13"
                        placeholder="请如实填写，关系成绩" v-model="user.number"/>
            </div>
          </div>
          <div class="form-btn-line">
            <div class="form-btn clearfix">
              <button class="button button-red-solid submit-btn pull-left"
                      style="margin-right: 50px"
                      @click="save('form')">{{ $t('personalInfo.save') }}
              </button>
              <button class="button button-red-hollow submit-btn pull-left"
                      @click="cancelForm">{{ $t('personalInfo.cancel') }}
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getOneInfoCollege} from "@/apis/people";
import {updateInfoCollege} from "@/apis/people";

export default {
  name: "college",
  data() {
    return {
      loading: true,
      user: {
        college: '',
        // 下面两个属性固定为-----，因为数据库中没有该信息
        grade: '',
        academy: '',
        classes: '',
        number: '',
      },
    }
  },
  created() {
    this.init()

  },
  methods: {
    // 信息初始化
    async init() {
      this.closeLoading()
      let id = localStorage.getItem("id");
      getOneInfoCollege(id).then((resp) => {
        if (resp?.data?.code == 1) {
          this.user.college = resp?.data?.data?.college
          this.user.classes = resp?.data?.data?.specialized + " " + resp?.data?.data?.classes
          this.user.grade = '-----'
          this.user.academy = '-----'
          this.user.number = resp?.data?.data?.number
          localStorage.setItem("number", this.user.number)
        } else if (resp?.data?.code == 0) {
          this.$router.push({
            name: 'login'
          }).catch(r => {
            console.log(r)
          })
        }
      })

    },
    // 表单验证，提交学校信息
    save() {
      // 由于没有表单，所以使用js方法来处理非空
      if (this.user.number.trim() === "" || this.user.number === "") {
        this.$message.error("学号不能为空")
      } else {
        let college = {
          number: this.user.number.trim(),
          original: localStorage.getItem("number")
        }
        updateInfoCollege(college).then((resp) => {
          if (resp?.data?.code == 1) {
            localStorage.setItem("number", resp?.data?.data)
            this.$message.success("修改信息成功")
          } else {
            this.$message.error(resp?.data?.msg)
          }

        })


      }

    },
    // 取消
    cancelForm() {
      let type = localStorage.getItem("type")
      if (type === "student") {
        this.$router.push({
          name: 'studentIndex'
        }).catch(err => {
          console.log(err)
        })
      }
      if (type === "teacher") {
        this.$router.push({
          name: 'teacherIndex',
        }).catch(err => {
          console.log(err)
        })
      }

    },
    // 加载动画
    closeLoading() {
      this.timer = null
      this.timer = setTimeout(() => {
        this.loading = false
      }, 500)
    }
  }
}
</script>

<style scoped>
*, :after, :before {
  box-sizing: border-box;
}

div {
  display: block;
}

.divTmp {
  width: 100%;
}

::v-deep .el-loading-mask {
  transition: opacity 0s;
}

::v-deep .el-loading-spinner {
  top: 100%;
  margin-top: 30%;
  width: 100%;
  text-align: center;
  position: absolute;
}
body, html {
  color: #444;
  font-size: 14px;
}

a {
  background-color: transparent;
  color: #337ab7;
  text-decoration: none;
}

a:active, a:hover {
  outline: 0;
}

.form-wrapper {
  margin: 30px auto;
  padding: 0 20px;
  max-width: 760px;
}

.user-info-title {
  text-align: right;
  margin-bottom: 15px;
  color: #ea5947;
  font-size: 16px;
  font-weight: 700;
  width: 100px;
}

.form-group {
  margin-bottom: 15px;
}

.user-page .user-info-page .form-group {
  margin-bottom: 20px;
  width: 100%;
  min-height: 32px;
  position: relative;
}

.user-info-label {
  float: left;
  text-align: right;
  margin-right: 20px;
  width: 100px;
  height: 32px;
}

.pull-left {
  float: left !important;

}

form {
  display: block;
  margin-top: 0em;
}

.user-page .user-info-page .form-group {
  margin-bottom: 20px;
  width: 100%;
  min-height: 32px;
  position: relative;
}

::v-deep .el-input__inner {
  border-radius: 4px;
  background-color: #fff;
  width: 76%;
  height: 32px;
  font-size: 14px;
  line-height: 32px;
}

.school {
  padding-left: 15px;
  color: rgba(173, 163, 162, 0.59);
}

.button-red-hollow {
  background-color: #fff;
  border-color: #e3e3e9;
  color: #7691fc;
}

.button-red-hollow:hover {
  border-color: #5c71c3;
}

.button-red-solid {
  background-color: #7691fc;
  border-color: #7691fc;
}


.submit-btn {

  display: block;
}


.form-btn-line {
  margin: 60px -20px;
  padding-top: 24px;
  border-top: 1px solid #e3e3e9;
}

.form-btn {
  width: 212px;
  margin: 0 auto;
  height: 32px;
}

</style>